package com.szewec.ps.ui.road.facilities.investigation.task.roadside.facilities;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSONObject;
import com.szewec.ps.R;
import com.szewec.ps.base.Enums;
import com.szewec.ps.databinding.FragmentRoadCrossingFacilitiesBinding;
import com.szewec.ps.model.road.facilities.investigation.road.side.facilities.RoadCrossingFacilities;
import com.szewec.ps.model.road.facilities.investigation.task.RoadFacilitiesInvestigationTask;
import com.szewec.ps.model.road.facilities.investigation.task.RoadFacilitiesInvestigationTaskDTO;
import com.szewec.ps.ui.road.facilities.intersection.task.views.RadioBoxView;
import com.szewec.ps.util.RollUtil;
import com.szewec.ps.util.StringUtils;
import com.szewec.ps.util.TextViewUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 过街设施
 */
public class RoadCrossingFacilitiesFragment extends Fragment {
    private FragmentRoadCrossingFacilitiesBinding binding;
    // 路段设施调查任务DTO
    private RoadFacilitiesInvestigationTaskDTO.RoadFacilitiesTaskDTO roadFacilitiesTaskDTO;
    private RoadCrossingFacilities roadCrossingFacilities;

    // 建造位置 数据字典
    private List<String> constructionLocationDict = new ArrayList<>();
    // 连接形式 数据字典
    private List<String> connectionFormDict = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_road_crossing_facilities, container, false);
        // 2、获取到视图
        View inflate = binding.getRoot();
        ButterKnife.bind(this, inflate);

        initData();
        initView();
        return inflate;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        // 通过Activity中的public方法来获取值
        roadFacilitiesTaskDTO = ((RoadsideFacilitiesActivity) context).pushFragmentData();
    }

    private void initData() {
        constructionLocationDict = Enums.ConstructionLocationEnum.getEnumValueList();
        connectionFormDict = Enums.ConnectionFormEnum.getEnumValueList();
        roadCrossingFacilities = roadFacilitiesTaskDTO.getRoadCrossingFacilities() == null ?
                new RoadCrossingFacilities() : roadFacilitiesTaskDTO.getRoadCrossingFacilities();
    }

    private void initView() {
        // 是否二次过街
        Integer crossTheStreetTwice = roadCrossingFacilities.getCrossTheStreetTwice() == null ? RadioBoxView.ONE : roadCrossingFacilities.getCrossTheStreetTwice();
        // 有无自行车道过街通道
        Integer bicycleCrossing = roadCrossingFacilities.getBicycleCrossing() == null ? RadioBoxView.ONE : roadCrossingFacilities.getBicycleCrossing();
        // 人行天桥 有无自行车道
        Integer cyclePathFootbridge = roadCrossingFacilities.getCyclePathFootbridge() == null ? RadioBoxView.ONE : roadCrossingFacilities.getCyclePathFootbridge();
        // 人行地道 有无自行车道
        Integer cyclePathTunnel = roadCrossingFacilities.getCyclePathTunnel() == null ? RadioBoxView.ONE : roadCrossingFacilities.getCyclePathTunnel();

        binding.crossTheStreetTwice.setChecked(crossTheStreetTwice);
        binding.sidewalkWidth.setValue(roadCrossingFacilities.getSidewalkWidth());
        binding.managementControl.setValue(roadCrossingFacilities.getManagementControl());
        binding.bicycleCrossing.setChecked(bicycleCrossing);
        binding.constructionLocationFootbridge.setValue(Enums.ConstructionLocationEnum.getValueByKey(roadCrossingFacilities.getConstructionLocationFootbridge()));
        binding.channelWidthFootbridge.setValue(roadCrossingFacilities.getChannelWidthFootbridge());
        binding.connectionFormFootbridge.setValue(Enums.ConnectionFormEnum.getValueByKey(roadCrossingFacilities.getConnectionFormFootbridge()));
        binding.cyclePathFootbridge.setChecked(cyclePathFootbridge);
        binding.constructionLocationTunnel.setValue(Enums.ConstructionLocationEnum.getValueByKey(roadCrossingFacilities.getConstructionLocationTunnel()));
        binding.channelWidthTunnel.setValue(roadCrossingFacilities.getChannelWidthTunnel());
        binding.connectionFormTunnel.setValue(Enums.ConnectionFormEnum.getValueByKey(roadCrossingFacilities.getConnectionFormTunnel()));
        binding.cyclePathTunnel.setChecked(cyclePathTunnel);
    }

    public RoadCrossingFacilities getData() {
        // 添加路段调查任务ID
        RoadFacilitiesInvestigationTask facilitiesInvestigationTask = roadFacilitiesTaskDTO.getFacilitiesInvestigationTask();
        if (facilitiesInvestigationTask != null && StringUtils.isNotEmpty(facilitiesInvestigationTask.getId())) {
            roadCrossingFacilities.setRoadInvestigationTaskId(facilitiesInvestigationTask.getId());
        }
        roadCrossingFacilities.setCrossTheStreetTwice(binding.crossTheStreetTwice.getChecked());
        roadCrossingFacilities.setSidewalkWidth(binding.sidewalkWidth.getBigDecimalValue());
        roadCrossingFacilities.setManagementControl(binding.managementControl.getStringValue());
        roadCrossingFacilities.setBicycleCrossing(binding.bicycleCrossing.getChecked());
        roadCrossingFacilities.setConstructionLocationFootbridge(Enums.ConstructionLocationEnum.getKeyByValue(binding.constructionLocationFootbridge.getStringValue()));
        roadCrossingFacilities.setChannelWidthFootbridge(binding.channelWidthFootbridge.getBigDecimalValue());
        roadCrossingFacilities.setConnectionFormFootbridge(Enums.ConnectionFormEnum.getKeyByValue(binding.connectionFormFootbridge.getStringValue()));
        roadCrossingFacilities.setCyclePathFootbridge(binding.cyclePathFootbridge.getChecked());
        roadCrossingFacilities.setConstructionLocationTunnel(Enums.ConstructionLocationEnum.getKeyByValue(binding.constructionLocationTunnel.getStringValue()));
        roadCrossingFacilities.setChannelWidthTunnel(binding.channelWidthTunnel.getBigDecimalValue());
        roadCrossingFacilities.setConnectionFormTunnel(Enums.ConnectionFormEnum.getKeyByValue(binding.connectionFormTunnel.getStringValue()));
        roadCrossingFacilities.setCyclePathTunnel(binding.cyclePathTunnel.getChecked());

        return roadCrossingFacilities;
    }

    @OnClick({R.id.constructionLocationFootbridge, R.id.connectionFormFootbridge, R.id.constructionLocationTunnel, R.id.connectionFormTunnel})
    public void onViewClicked(View view) {
        //隐藏系统软键盘
        TextViewUtil.closeKeyboard(getActivity());
        switch (view.getId()) {
            case R.id.constructionLocationFootbridge:
                RollUtil.alertBottomWheelOption(getContext(), constructionLocationDict, binding.constructionLocationFootbridge.getSelectTextView(), new RollUtil.OnWheelViewClick() {
                    @Override
                    public void onClick(View view, int index) {
                        binding.constructionLocationFootbridge.setValue(constructionLocationDict.get(index));
                    }
                });
                break;
            case R.id.connectionFormFootbridge:
                RollUtil.alertBottomWheelOption(getContext(), connectionFormDict, binding.connectionFormFootbridge.getSelectTextView(), new RollUtil.OnWheelViewClick() {
                    @Override
                    public void onClick(View view, int index) {
                        binding.connectionFormFootbridge.setValue(connectionFormDict.get(index));
                    }
                });
                break;
            case R.id.constructionLocationTunnel:
                RollUtil.alertBottomWheelOption(getContext(), constructionLocationDict, binding.constructionLocationTunnel.getSelectTextView(), new RollUtil.OnWheelViewClick() {
                    @Override
                    public void onClick(View view, int index) {
                        binding.constructionLocationTunnel.setValue(constructionLocationDict.get(index));
                    }
                });
                break;
            case R.id.connectionFormTunnel:
                RollUtil.alertBottomWheelOption(getContext(), connectionFormDict, binding.connectionFormTunnel.getSelectTextView(), new RollUtil.OnWheelViewClick() {
                    @Override
                    public void onClick(View view, int index) {
                        binding.connectionFormTunnel.setValue(connectionFormDict.get(index));
                    }
                });
        }
    }
}